package Base;



import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.imageio.ImageIO;

/**
 * This class inherits from SceneElement and it represents figures like ginat and semar
 * @author Wayang Group Bremen
 * @version 0.9
 */
public class Figure extends SceneElement{

	public String path;
	public String path_flipped;
	public String path_black;
	public String path_black_flipped;
	public String path_sound;
	//pathRotL
	public String pathRot;
	public String pathRot2;
	public String pathRot3;
	public String pathRot4;
	//pathRotFlipped
	public String pathRotF2;
	public String pathRotF3;
	public String pathRotF4;
	//pathRotBWFlipped
	public String pathRotFBw2;
	public String pathRotFBw3;
	public String pathRotFBw4;
	//pathRotBW
	public String pathRotBw2;
	public String pathRotBw3;
	public String pathRotBw4;



	//Konstruktor
	public Figure () {

	}

	/**
	 * Constructor. Creates a new object of this class.
	 * @param name the title of the figure
	 * @param path the path to its image
	 * @param flipped boolean value that determines if the figure is flipped. Is true, if it is
	 * @param sound boolean value that determines, if the figure has a sound
	 * @param rotation boolean value that determines, if the the figure is rotated 
	 * @param Z the Z-buffer, that determines in which layer the object is in.
	 */
	public Figure(String name, String path, boolean flipped, boolean sound, boolean rotation, int Z) {
		this.name = name;
		this.text = null;
		this.path = path;
		this.flipped = flipped;
		this.blackwhite = false;
		this.sound2 = sound;
		this.Z = Z;
		this.hasFocus = false;
		this.isDragged = false;
		this.animationStates = new Vector<AnimationState>();
		try {
			this.image =  ImageIO.read(new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.rect = new Rectangle(50, 50, image.getWidth(), image.getHeight());
		this.hide.x = this.rect.x;
		this.hide.y = this.rect.y;
		this.isFigure = true;
		this.backGroundPath = "";

	}

	/**
	 * This method returns the name of the figure. If it is bold, it returns bold.
	 * @return the name of the figure
	 */
	public String toString() {
		String tempName="";
		if(hasFocus) {
			tempName = "<html><b> "+ name+"</b></html>";
		}
		else {
			tempName = name;
		}
		return tempName;
	}


	/** 
	 * This method creates paths to the images and the images.
	 */
	public void createImagePaths() {
		//this.backGroundPath = "\\figuresicons\\".concat(path);
		backGroundPath = "Data//figuresicons//" .concat(name).concat(".png");

		//System.out.println("backgroundPath:" +  backGroundPath);


		if(path_flipped == null) {
			String append = "f.";
			Vector<String> image_flip = new Vector<String>();
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				image_flip.add(st.nextToken());

			}
			image_flip.add(1, append);
			path_flipped = "";

			for (int i=0; i<image_flip.size();i++) {
				path_flipped = path_flipped.concat(image_flip.get(i));
			} 
		}

		if(path_black_flipped == null ) {
			String appendbwf = "bwf.";
			Vector<String> image_bwf = new Vector<String>();

			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				image_bwf.add(st.nextToken());

			}
			image_bwf.add(1, appendbwf);
			path_black_flipped = "";

			for (int i=0; i<image_bwf.size();i++) {
				path_black_flipped = path_black_flipped.concat(image_bwf.get(i));
			}
		}
		if (path_black == null) {
			String appendbw = "bw.";
			Vector<String> image_bw = new Vector<String>();

			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				image_bw.add(st.nextToken());

			}
			image_bw.add(1, appendbw);
			path_black = "";

			for (int i=0; i<image_bw.size();i++) {
				path_black = path_black.concat(image_bw.get(i));
			}
		}
		//RotLeft
		if(pathRot2 == null) {
			String appendrot2 = "-rr2.";
			Vector<String> imageRot2 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRot2.add(st.nextToken());

			}
			imageRot2.add(1,appendrot2);
			pathRot2 = "";
			for (int i=0; i<imageRot2.size();i++) {
				pathRot2 = pathRot2.concat(imageRot2.get(i));
			}
		}

		if(pathRot3 == null) {
			String appendrot3 = "-rr3.";
			Vector<String> imageRot3 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRot3.add(st.nextToken());

			}
			imageRot3.add(1,appendrot3);
			pathRot3 = "";
			for (int i=0; i<imageRot3.size();i++) {
				pathRot3 = pathRot3.concat(imageRot3.get(i));
			}
		}
		if(pathRot4 == null) {
			String appendrot4 = "-rr4.";
			Vector<String> imageRot4 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRot4.add(st.nextToken());

			}
			imageRot4.add(1,appendrot4);
			pathRot4 = "";
			for (int i=0; i<imageRot4.size();i++) {
				pathRot4= pathRot4.concat(imageRot4.get(i));
			}
		}
		//RotRight
		if(pathRotF2 == null) {
			String appendrotF2 = "f-rr2.";
			Vector<String> imageRotF2 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotF2.add(st.nextToken());

			}
			imageRotF2.add(1,appendrotF2);
			pathRotF2 = "";
			for (int i=0; i<imageRotF2.size();i++) {
				pathRotF2 = pathRotF2.concat(imageRotF2.get(i));
			}
		}
		if(pathRotF3 == null) {
			String appendrotF3 = "f-rr3.";
			Vector<String> imageRotF3 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotF3.add(st.nextToken());

			}
			imageRotF3.add(1,appendrotF3);
			pathRotF3 = "";
			for (int i=0; i<imageRotF3.size();i++) {
				pathRotF3 = pathRotF3.concat(imageRotF3.get(i));
			}
		}
		if(pathRotF4 == null) {
			String appendrotF4 = "f-rr4.";
			Vector<String> imageRotF4 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotF4.add(st.nextToken());

			}
			imageRotF4.add(1,appendrotF4);
			pathRotF4 = "";
			for (int i=0; i<imageRotF4.size();i++) {
				pathRotF4 = pathRotF4.concat(imageRotF4.get(i));
			}
		}
		//roateBwFlipped
		if(pathRotFBw2 == null) {
			String appendrotBwF2 = "bwf-rr2.";
			Vector<String> imageRotBwF2 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotBwF2.add(st.nextToken());

			}
			imageRotBwF2.add(1,appendrotBwF2);
			pathRotFBw2 = "";
			for (int i=0; i<imageRotBwF2.size();i++) {
				pathRotFBw2 = pathRotFBw2.concat(imageRotBwF2.get(i));
			}
		}
		if(pathRotFBw3 == null) {
			String appendrotBwF3 = "bwf-rr3.";
			Vector<String> imageRotBwF3 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotBwF3.add(st.nextToken());

			}
			imageRotBwF3.add(1,appendrotBwF3);
			pathRotFBw3 = "";
			for (int i=0; i<imageRotBwF3.size();i++) {
				pathRotFBw3 = pathRotFBw3.concat(imageRotBwF3.get(i));
			}
		}
		if(pathRotFBw4 == null) {
			String appendrotBwF4 = "bwf-rr4.";
			Vector<String> imageRotBwF4 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotBwF4.add(st.nextToken());

			}
			imageRotBwF4.add(1,appendrotBwF4);
			pathRotFBw4 = "";
			for (int i=0; i<imageRotBwF4.size();i++) {
				pathRotFBw4 = pathRotFBw4.concat(imageRotBwF4.get(i));
			}
		}
		//rotate BW
		if(pathRotBw2 == null) {
			String appendrotBw2 = "bw-rr2.";
			Vector<String> imageRotBw2 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotBw2.add(st.nextToken());

			}
			imageRotBw2.add(1,appendrotBw2);
			pathRotBw2 = "";
			for (int i=0; i<imageRotBw2.size();i++) {
				pathRotBw2 = pathRotBw2.concat(imageRotBw2.get(i));
			}
		}
		if(pathRotBw3 == null) {
			String appendrotBw3 = "bw-rr3.";
			Vector<String> imageRotBw3 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotBw3.add(st.nextToken());

			}
			imageRotBw3.add(1,appendrotBw3);
			pathRotBw3 = "";
			for (int i=0; i<imageRotBw3.size();i++) {
				pathRotBw3 = pathRotBw3.concat(imageRotBw3.get(i));
			}
		}
		if(pathRotBw4 == null) {
			String appendrotBw4 = "bw-rr4.";
			Vector<String> imageRotBw4 = new Vector<String>(); 
			StringTokenizer st = new StringTokenizer(path, ".");
			while (st.hasMoreTokens()) { 
				imageRotBw4.add(st.nextToken());

			}
			imageRotBw4.add(1,appendrotBw4);
			pathRotBw4 = "";
			for (int i=0; i<imageRotBw4.size();i++) {
				pathRotBw4 = pathRotBw4.concat(imageRotBw4.get(i));
			}
		}


		try {
			this.imageBlack =  ImageIO.read(new File(path_black));
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageBlack_Flipped =  ImageIO.read(new File(path_black_flipped));
		} catch (IOException e) {
		} 
		try {
			this.image_Flipped =  ImageIO.read(new File(path_flipped));
		} catch (IOException e) {
			e.printStackTrace();
		} 
		//rotate 
		try {
			this.imageRot2 =  ImageIO.read(new File(pathRot2));
			imagesRotate.add(imageRot2);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRot3 =  ImageIO.read(new File(pathRot3));
			imagesRotate.add(imageRot3);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRot4 =  ImageIO.read(new File(pathRot4));
			imagesRotate.add(imageRot4);
			//imagesRotate.add(image);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		//rotate Flip
		try {
			this.imageRotF2 =  ImageIO.read(new File(pathRotF2));
			imagesRotateF.add(imageRotF2);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRotF3 =  ImageIO.read(new File(pathRotF3));
			imagesRotateF.add(imageRotF3);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRotF4 =  ImageIO.read(new File(pathRotF4));
			imagesRotateF.add(imageRotF4);
			//imagesRotate.add(image);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		//rotate flipped black
		try {
			this.imageRotFBw2 =  ImageIO.read(new File(pathRotFBw2));
			imagesRotateFBW.add(imageRotFBw2);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRotFBw3 =  ImageIO.read(new File(pathRotFBw3));
			imagesRotateFBW.add(imageRotFBw3);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRotFBw4 =  ImageIO.read(new File(pathRotFBw4));
			imagesRotateFBW.add(imageRotFBw4);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//rotate Black
		try {
			this.imageRotBw2 =  ImageIO.read(new File(pathRotBw2));
			imagesRotateBW.add(imageRotBw2);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRotBw3 =  ImageIO.read(new File(pathRotBw3));
			imagesRotateBW.add(imageRotBw3);
		} catch (IOException e) {
			e.printStackTrace();
		} 
		try {
			this.imageRotBw4 =  ImageIO.read(new File(pathRotBw4));
			imagesRotateBW.add(imageRotBw4);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}			
}
